package com.insurance.service.impl;

import com.insurance.common.Contants;
import com.insurance.common.ResultData;
import com.insurance.mapper.OrderPayMapper;
import com.insurance.pojo.PayInfo;
import com.insurance.pojo.Refund;
import com.insurance.service.OrderPayService;
import com.insurance.utils.ConversionDateUtils;
import com.insurance.utils.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 朱雄
 * Date: 2020/10/31 17:18
 * Description:
 * Version: V1.0
 */
@Service
public class OrderPayServiceImpl implements OrderPayService {

    @Autowired
   private OrderPayMapper payMapper;

    @Override
    public ResultData updatePayOrder(PayInfo payInfo) {
        ResultData resultData=new ResultData();
        int res2= payMapper.payOrder(payInfo);
        //先判断用户是否使用了优惠券
        if (payInfo.getCoupon_id()!=null){
        int coupon=payMapper.deleteCoupon(payInfo.getCoupon_id(),payInfo.getUser_id());
        }
        if (res2>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("支付成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("支付失败");
        }
        return resultData;
    }

    @Override
    public ResultData updateSuceessPayOrder(PayInfo payInfo) {
        ResultData resultData=new ResultData();
        //支付状态
        int res2= payMapper.paySuceessOrder(payInfo);
        //订单状态
        int res=payMapper.updateOrderStartu(payInfo.getOrderNumber());
        //通过支付id查询司机信息
        Map map=payMapper.queryDriverInfo(payInfo.getOrderNumber());
        if (res>0){
            //支付成功后给司机发邮箱提醒
            if (map.get("user_email")!=null){
            EmailUtil emailUtil=new EmailUtil(map.get("user_email").toString(),"您有新的订单，请注意查看！！！",map.get("user_name").toString());
            emailUtil.run();
            }
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("支付成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("支付失败");
        }
        return resultData;
    }


    @Override
    public ResultData updateRefund(PayInfo payInfo) {
        ResultData resultData=new ResultData();
        int res2= payMapper.refundOrder(payInfo);
        int res=payMapper.updateRefundOrderStartu(payInfo.getOrderNumber());
        if (res>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("退款成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("退款失败");
        }
        return resultData;
    }

    @Override
    public ResultData queryRefundOrder(Integer orderStart) {
        ResultData resultData=new ResultData();
        List<Map> list=payMapper.queryRefundOrder(orderStart);
        if (list.size()>0){
            for (Map map:list){
                map.put("application_time", ConversionDateUtils.conversionDateStringUtils(map.get("application_time")));
                map.put("order_time", ConversionDateUtils.conversionDateStringUtils(map.get("order_time")));
            }
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("查询成功");
            resultData.setData(list);
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("无退款信息");
        }
        return resultData;
    }
    @Override
    public ResultData addApplyRefundByOrder(Refund refund) {
        ResultData resultData=new ResultData();

        //int del=payMapper.deleteRefundByOrder(refund);
        int res=payMapper.addApplyRefundByOrder(refund);
        //修改订单状态为退款中
        int res1=payMapper.addRefundOrderStartu(refund);
        if (res1>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("退款申请成功");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("退款申请失败");
        }
        return resultData;
    }

    /**
     * 驳回
     * @param refund
     * @return
     */
    @Override
    public ResultData noAgreeExitPayOrder(Refund refund) {
        ResultData resultData=new ResultData();
        int ref=payMapper.noAgreeExitPayOrder(refund);
        int reorder=payMapper.noAgreerejectRefund(refund);
        if (reorder>0){
            resultData.setCode(Contants.SUCCESS);
            resultData.setMsg("成功驳回退款申请");
        }else {
            resultData.setCode(Contants.FAILURE);
            resultData.setMsg("驳回退款申请失败");
        }
        return resultData;
    }
}
